package com.xgs.common.utils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 海康监控摄像头用户名密码加解密工具类
 *
 * @author Victor.Qiao
 */
public class HKVisionEncryptUtils {


    /**
     * xgs带中文加密算法
     *
     * @param Src     需要加密的字符串
     * @param Encrypt True为加密，False为解密
     * @param zKey    种子
     * @return
     */
    public static String encrypt(String Src, Boolean Encrypt, String zKey) {
        String strEnc = "UTF-8";

        if (StringUtils.isEmpty(Src)) {
            return "";
        }

        int KeyLen, KeyPos, offset, SrcPos, SrcAsc, TmpSrcAsc, Range, hexTemp;
        KeyLen = zKey.length();
        if (KeyLen == 0) {
            zKey = "Hello";
        }
        KeyPos = 0;
        Range = 256;
        if (Encrypt) {
            try {
                String Desc = "";
                String sTemp = "";
                Random rd = new Random(Range);
                offset = rd.nextInt(255);
                Desc = String.format("%02x", offset);

                byte[] chrSrc = new byte[0];
                byte[] bytezKey;
                char[] chrzKey = new char[0];
                try {
                    chrSrc = Src.getBytes(Charset.forName(strEnc));
                    bytezKey = zKey.getBytes(Charset.forName(strEnc));
                    chrzKey = new String(bytezKey, Charset.forName(strEnc)).toCharArray();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                for (SrcPos = 1; SrcPos <= chrSrc.length; SrcPos++) {
                    SrcAsc = (chrSrc[SrcPos - 1] + offset) % 255;
                    SrcAsc = SrcAsc ^ chrzKey[KeyPos];
                    if (KeyPos < KeyLen - 1) {
                        KeyPos++;
                    } else {
                        KeyPos = 0;
                    }
                    Desc += String.format("%02x", SrcAsc);
                    offset = SrcAsc;
                }


                sTemp = "";
                for (SrcPos = 1; SrcPos <= Desc.length() / 2; SrcPos++) {
                    sTemp = sTemp + Desc.substring(Desc.length() - (SrcPos), Desc.length() - (SrcPos) + 1) + Desc.substring(SrcPos - 1, SrcPos);
                }

                hexTemp = 0;
                for (SrcPos = 0; SrcPos <= sTemp.length() / 2 - 1; SrcPos++) {
                    hexTemp = hexTemp ^ Integer.parseInt(sTemp.substring(SrcPos * 2, SrcPos * 2 + 2), 16);
                }

                sTemp = String.format("%02x", hexTemp) + sTemp;

                Desc = sTemp.toUpperCase();
                return Desc;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                List<Byte> listDesc = new ArrayList<>();

                hexTemp = 0;
                for (SrcPos = 0; SrcPos < Src.length() / 2 - 1; SrcPos++) {
                    hexTemp = hexTemp ^ Integer.parseInt(Src.substring(SrcPos * 2 + 2, SrcPos * 2 + 2 + 2), 16);
                }

                if (!(Src.substring(0, 2)).equalsIgnoreCase(String.format("%02x", hexTemp))) {
                    return "";
                }

                String sTemp = "";
                for (SrcPos = Src.length() / 2 - 1; SrcPos >= 1; SrcPos--) {
                    sTemp = Src.substring(SrcPos * 2 + 1, SrcPos * 2 + 1 + 1) + sTemp + Src.substring(SrcPos * 2, SrcPos * 2 + 1);
                }

                offset = Integer.parseInt(sTemp.substring(0, 2), 16);

                byte[] bytezKey;
                char[] chrzKey = new char[0];
                try {
                    bytezKey = zKey.getBytes(Charset.forName(strEnc));
                    chrzKey = new String(bytezKey, Charset.forName(strEnc)).toCharArray();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                SrcPos = 2;
                do {
                    SrcAsc = Integer.parseInt(sTemp.substring(SrcPos, SrcPos + 2), 16);
                    TmpSrcAsc = SrcAsc ^ chrzKey[KeyPos];
                    if (KeyPos < KeyLen - 1) {
                        KeyPos++;
                    } else {
                        KeyPos = 0;
                    }
                    if (TmpSrcAsc <= offset) {
                        TmpSrcAsc = 255 + TmpSrcAsc - offset;
                    } else {
                        TmpSrcAsc = TmpSrcAsc - offset;
                    }
                    listDesc.add((byte) TmpSrcAsc);
                    offset = SrcAsc;
                    SrcPos = SrcPos + 2;
                }
                while (SrcPos < sTemp.length());


                byte[] byteArrDesc = new byte[listDesc.size()]; // 将 List 转换为 byte 数组
                for (int i = 0; i < listDesc.size(); i++) {
                    byteArrDesc[i] = listDesc.get(i);
                }

                //String strDesc = new String(byteArrDesc, strEnc); // 使用 String 的 getBytes 方法进行转换
                String strDesc = new String(byteArrDesc, Charset.forName(strEnc));

                return strDesc;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }


    public static void main(String args[]) {
        System.out.println(encrypt("E202795FE9D066F0EA501AC009", false, "xgs"));
    }
}
